#include "utils.h"

#ifndef LZW_H
#define LZW_H

using namespace std;

#define DICTIONARY_MAXSIZE 4096

/**
 * \class LZW
 *
 * \brief LZW algorithm used in GIF. 
 *
 * \author Maksym Rybak
 * \date 17.03.2011
**/
class LZW{	

	BYTE InitialCodeSize;					///< Number of bits needed to represent the actual data
	int currentDecodedIndex;				///< The sequence number of the current decoded Pixel Index

	string *dictionary;						///< LZW Dictionary (String table)
	int dictionaryCurrentEntry;				///< The number of the last entry in Dictionary 	

	int bitCodeBuffer;						///< Bit buffer for read one bit at time from encoded byte
	int bitCodeBufferPos;					///< The number of bits inside bitCodeBuffer

	// Word variables used by LZW GIF Decoding algorithm
	int code;								///< Current code
	string c;								///< String of Pixel Indices			
	string old;								///< String of Pixel Indices read one step before
	int codesize;							///< Current code size (MAX 12 bits)
	int ClearCode;							///< Clear Code = 2^InitialCodeSize	
	int EOICode;							///< End Of Image Code = ClearCode + 1
	bool restart;							///< If true: ClearCode read, reinitialize dictionary and codesize
	
	void initializeDictionary();
public:
	LZW();
	~LZW();

	void setInitialCodeSize(BYTE CodeSize);
	int calcMaxNum(int bitNum);								
	void initializeAllWorkVariables();
	bool readGIFDataBlock(BYTE *encodedBlock, int blockSize, BYTE *imageIndices);
};

#endif /* LZW_H */